import socket  
import struct
import rpcmsg_pb2
import time
import math




client = socket.socket( socket.AF_INET, socket.SOCK_DGRAM )

#set socket timeout
client.settimeout(2.0) 

svr_addr = ('localhost', 2161) 

i=0.0

try:
    while 1:   
        pi_points = rpcmsg_pb2.PIPoints()

        pi_point = pi_points.pts.add()
        pi_point.pt = 14187
        pi_point.val = math.sin(i)*100
        #pi_point.val = 6.0

        pi_point.timestamp = 0
        
        print pi_points
        
        data_field = pi_points.SerializeToString()
        
        longs = [0,105,0,0,0,0,0,0]
        longs[7] = len(data_field)

        msg_head = struct.pack('8i',*longs)    
        
   
        send_rs = client.sendto( msg_head+data_field, svr_addr ) 
        print 'send', send_rs, 'bytes'
        
        i = i+1.0/180.0
        
        #time.sleep(1)
    

        
except socket.error, msg:
    print msg
    
print 'end'